layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var searchType = $("#searchType").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/activity/jsonBrandList',
        where: {searchType:searchType},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25,2000],
        id : "entityList",
        cols : [[
            {type:'checkbox'},
            {field: 'name', title: '名称',align:'center'},
            {field: 'catName', title: '分类名',align:'center'},
            {field: 'itemCountNum', title: '商品数',align:'center'},
            {field: 'priceRecord', title: '加价',align:'center'},
            {field: 'profitRecord', title: '利润',align:'center'},
            {field: 'logoImage', title: 'LOGO',align:'center', templet:function(d){
                return "<img src='"+d.logoImage+"' style='width: 60px; height: 60px;'/>";
            }},
            {field: 'orderNum', title: '排序',width:70,align:'center',templet:function(d){
                if(d.orderNum==100000){
                    return "置顶";
                }else{
                    return d.orderNum;
                }
            }},
            {field: 'status', title: '状态',width:120,align:'center',templet:function(d){
                if(d.status=="1"){
                    return "有效";
                }else if(d.status=="2"){
                    return "无效";
                }
            }},
            {field: 'createTime', title: '创建时间', align:'center', minWidth:110, templet:function(d){
                return d.createTime;
            }},
            {title: '操作',width:250,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs" lay-event="up">上移</a>\
                <a class="layui-btn layui-btn-xs" lay-event="down">下移</a>\
                <a class="layui-btn layui-btn-xs" lay-event="top">置顶</a>\
                    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>\
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                catId: $("#catId").val(),
                brandName:$("#brandName").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            area: ['300px', '250px'],
            content : "/brand/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                });
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    });

    $(".add_price_btn").click(function(){
        // 获取所有的ids
        var checkStatus = layui.table.checkStatus('entityList').data;

        if(checkStatus.length == 0){
            layer.alert("请选择品牌！")
            return false;
        }
        if(checkStatus.length > 1){
            layer.alert("请选择一个品牌！")
            return false;
        }
        var ids = [];
        $.each(checkStatus,function(i,v){
            ids.push(v.id);
        });
        console.log(ids.join(","));
        var index = layui.layer.open({
            title :"品牌加价",
            type : 2,
            content : "/item/selectBrandPriceEntity?brandId="+ids.join(","),
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                // layui.layer.full(index);
            }
        });
    });

    $(".add_profit_btn").click(function(){
        // 获取所有的ids
        var checkStatus = layui.table.checkStatus('entityList').data;

        if(checkStatus.length == 0){
            layer.alert("请选择品牌！")
            return false;
        }
        var ids = [];
        $.each(checkStatus,function(i,v){
            ids.push(v.id);
        });
        var index = layui.layer.open({
            title :"品牌加利润",
            type : 2,
            content : "/item/selectBrandProfitEntity?brandIds="+ids.join(","),
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                // layui.layer.full(index);
            }
        });
    });


    $(".cat_btn").click(function(){
        // 获取所有的ids
        var checkStatus = layui.table.checkStatus('entityList').data;

        if(checkStatus.length == 0){
            layer.alert("请选择至少一个品牌！")
            return false;
        }

        var ids = [];
        $.each(checkStatus,function(i,v){
            ids.push(v.id);
        });
        console.log(ids.join(","));
        var index = layui.layer.open({
            title :"品牌分类",
            type : 2,
            content : "/brand/selectBrandCatEntity?brandIds="+ids.join(","),
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                // layui.layer.full(index);
            }
        });
    });

    $(".add_profit_price_cat_btn").click(function(){
        // 获取所有的ids
        var checkStatus = layui.table.checkStatus('entityList').data;

        if(checkStatus.length == 0){
            layer.alert("请选择至少一个品牌！")
            return false;
        }

        var ids = [];
        var activityIds = [];
        var flag = true;
        $.each(checkStatus,function(i,v){
            ids.push(v.id);
            activityIds.push(v.activityId);
            if (v.itemCountNum <= 0){
                flag = false;
                return false;
            }
        });
        if (!flag){
            layer.alert("您选择的品牌必须有商品！")
            return false;
        }
        // console.log(ids.join(","));
        var index = layui.layer.open({
            title :"品牌分类",
            type : 2,
            content : "/item/selectBrandProfitCatEntity?brandIds="+ids.join(",")+"&activityIds="+activityIds.join(","),
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    });

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除品牌【'+data.name+'】吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/brand/deleteEntity",{
                    "id":data.id,
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        }else if(layEvent === 'up'){ //上移
            var order=data.orderNum;
            if(order>=100000){
                order=100000;
            }else{
                order=order*1+1;
            }
            updateOrderNum(data.id,order);
        }else if(layEvent === 'down'){ //下移
            var order=data.orderNum;
            if(order<=0){
                order=0;
            }else if(order>=100000){
                order=1;
            }else{
                order=order*1-1;
            }
            updateOrderNum(data.id,order);
        }else if(layEvent === 'top'){ //置顶
            updateOrderNum(data.id,100000);
        }
    });
    //排序处理
    function updateOrderNum(id,orderNum) {
        $.post("/brand/updateOrderNum",{
            id : id,
            orderNum :orderNum
        },function(result){
            if(1==result.returnInfo.state){
                tableIns.reload();
                layer.close(index);
            }else{
                top.layer.msg(result.returnInfo.errorMsg);
            }
        },"json")
    }

})